/**
 * @file:          motor_drv42_v11.h
 * @brief:
 * @details:
 * @author:        wjh
 * @date created:  2023.08.14
 * @version:       1.0.0
 * @par copyright (c):
 *
 * @par history (desc):
 *   version:1.0.0, wjh, 2023.08.14,11:14:31
 */

#ifndef _MOTOR_DRV42_H_
#define _MOTOR_DRV42_H_

/* Include Files **************************************************************/
#include "Datatype.h"
#include "ch32v30x_module.h"

/* Macro Definition ***********************************************************/

/* Data Type Define ***********************************************************/
typedef struct Motor_drv42
{
    /* public data */
    Mcu *mcu;

    float32 param_power_supply_V;
    float32 param_R_of_current_sample_ohm;
    float32 param_adc_ref_V;
    float param_power_voltage_sample_gain;
    float32 param_amp_gain;
    int16 param_adc_calibration_start_index;
    int16 param_adc_calibration_buffer_size;
    float32 param_led_toggle_period_s;
    int16 param_current_dir;
    uint16 param_pwm_output_en;
    float param_mos_Ron;                    /* unit: ohm */
    float param_mos_deadtime;               /* unit: s */
    float param_mos_rise_fall_time;         /* unit: s */
    float param_mos_max_peak_current;       /* unit: A */
    float param_mos_max_continuous_current; /* unit: A */

    /* v1.1 */
    // GpioGeneralType param_pin_LED;
    // GpioGeneralType param_pin_OSCIN;
    // GpioGeneralType param_pin_OSCOUT;
    // GpioGeneralType param_pin_OP2_B_OUT;
    // GpioGeneralType param_pin_OP1_A_OUT;
    // GpioGeneralType param_pin_MT_GSENSOR_CS;
    // GpioGeneralType param_pin_SPI_CLK;
    // GpioGeneralType param_pin_SPI_MISO;
    // GpioGeneralType param_pin_SPI_MOSI;
    // GpioGeneralType param_pin_UART_TX;
    // GpioGeneralType param_pin_UART_RX;
    // GpioGeneralType param_pin_OP2_N_IN;
    // GpioGeneralType param_pin_OP1_N_IN;
    // GpioGeneralType param_pin_OP2_P_IN;
    // GpioGeneralType param_pin_OP1_P_IN;
    // GpioGeneralType param_pin_PWM_IN_A1;
    // GpioGeneralType param_pin_PWM_IN_A2;
    // GpioGeneralType param_pin_PWM_IN_B1;
    // GpioGeneralType param_pin_PWM_IN_B2;
    // GpioGeneralType param_pin_SWD;
    // GpioGeneralType param_pin_SWC;
    // GpioGeneralType param_pin_STEP;
    // GpioGeneralType param_pin_DIR;
    // GpioGeneralType param_pin_EN;
    // GpioGeneralType param_pin_EMERGENCY;
    // GpioGeneralType param_pin_TestGpio;
    // GpioGeneralType param_pin_Vin;

    /* v1.2 */
    // // GpioGeneralType param_pin_LED;
    // // GpioGeneralType param_pin_OSCIN;
    // // GpioGeneralType param_pin_OSCOUT;
    // GpioGeneralType param_pin_CAL_EN;
    // // GpioGeneralType param_pin_OP2_B_OUT;
    // // GpioGeneralType param_pin_OP1_A_OUT;
    // // GpioGeneralType param_pin_MT_GSENSOR_CS;
    // // GpioGeneralType param_pin_SPI_CLK;
    // // GpioGeneralType param_pin_SPI_MISO;
    // // GpioGeneralType param_pin_SPI_MOSI;
    // GpioGeneralType param_pin_STEP_B1;
    // GpioGeneralType param_pin_STEP_B0;
    // // GpioGeneralType param_pin_PWM_IN_A1;
    // // GpioGeneralType param_pin_PWM_IN_A2;
    // // GpioGeneralType param_pin_PWM_IN_B1;
    // // GpioGeneralType param_pin_PWM_IN_B2;
    // GpioGeneralType param_pin_STEP_A1;
    // // GpioGeneralType param_pin_SWD;
    // // GpioGeneralType param_pin_SWC;
    // GpioGeneralType param_pin_STEP_A0;
    // // GpioGeneralType param_pin_EMERGENCY;
    // // GpioGeneralType param_pin_STEP;
    // // GpioGeneralType param_pin_EN;
    // // GpioGeneralType param_pin_UART_TX;
    // // GpioGeneralType param_pin_UART_RX;
    // // GpioGeneralType param_pin_DIR;
    // // GpioGeneralType param_pin_TestGpio;

    /*  */
    GpioGeneralType param_pin_STEP;
    GpioGeneralType param_pin_DIR;
    GpioGeneralType param_pin_EN;
    GpioGeneralType param_pin_STALL_OUT;
    GpioGeneralType param_pin_BUZZER_EN;
    GpioGeneralType param_pin_MOTER_PWR_EN;
    GpioGeneralType param_pin_UART1_TX;
    GpioGeneralType param_pin_UART1_RX;
    GpioGeneralType param_pin_ADC_VOUT;
    GpioGeneralType param_pin_ADC_VIN;
    GpioGeneralType param_pin_KEY1;
    GpioGeneralType param_pin_KEY2;
    GpioGeneralType param_pin_START;
    GpioGeneralType param_pin_STOP;
    GpioGeneralType param_pin_UART_TX;
    GpioGeneralType param_pin_UART_RX;
    GpioGeneralType param_pin_LED_GREEN;
    GpioGeneralType param_pin_LED_RED;
    GpioGeneralType param_pin_LED_STATUS1;
    GpioGeneralType param_pin_LED_STATUS2;

    float32 in_pwm_duty_A1;
    float32 in_pwm_duty_A2;
    float32 in_pwm_duty_B1;
    float32 in_pwm_duty_B2;

    int16 param_Ia_adc_offset;
    int16 param_Ib_adc_offset;
    int16 Ia_adc_value;
    int16 Ib_adc_value;
    uint32 led_toggle_period_count;
    uint32 led_toggle_count;
    float board_detect_power_supply_V;
    float board_detect_temperature_C;

    /* private data */

    /* public method */
    void (*ConfigParam)(struct Motor_drv42 *self);
    uint8 (*Init)(struct Motor_drv42 *self);
    void (*LedToggle)(struct Motor_drv42 *self);
    uint8 (*AdcCalibration)(struct Motor_drv42 *self);
    float32 (*Adc2Current)(struct Motor_drv42 *self, int16 adc_val);
    float32 (*GetIaFdb)(struct Motor_drv42 *self);
    float32 (*GetIbFdb)(struct Motor_drv42 *self);
    void (*UpdatePWM)(struct Motor_drv42 *self);
    void (*TestGpio_Set)(struct Motor_drv42 *self);
    void (*TestGpio_Reset)(struct Motor_drv42 *self);
    float (*GetPowerVoltage)(struct Motor_drv42 *self);
    float (*GetMcuTemperature)(struct Motor_drv42 *self);

    /* private method */

} Motor_drv42;

/* Variable Declare ***********************************************************/

/* Function Declare ***********************************************************/
uint8 Motor_drv42_v11_Init(struct Motor_drv42 *self);
uint8 Motor_drv42_v12_Init(struct Motor_drv42 *self);

#endif